package com.example.mobilesafe.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.mobilesafe.R;
import com.example.mobilesafe.constant.ConstantValue;
import com.example.mobilesafe.utils.SharedPreferencesUtil;
import com.example.mobilesafe.utils.ToastUtil;

public class SetupThreeActivity extends BaseSetupActivity {

    /**
     * "电话号码"编辑框
     */
    private EditText et_phone_number;

    /**
     * "选择联系人"按钮
     */
    private Button btn_select_number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_three);

        // 初始化UI
        initUI();
    }

    @Override
    protected void showNextPage() {
        // 点击按钮以后，需要获取输入框中的联系人，再做下一页操作
        String phone = et_phone_number.getText().toString();
        // sp中存储了相关联系人以后才可以跳转到下一个界面
        // String contact_phone = SharedPreferencesUtil.getString(getApplicationContext(), ConstantValue.CONTACT_PHONE, "");
        if (!TextUtils.isEmpty(phone)){
            Intent intent = new Intent(getApplicationContext(), SetupFourActivity.class);
            startActivity(intent);
            finish();
            // 如果是输入电话号码，同样需要保存
            SharedPreferencesUtil.putString(getApplicationContext(), ConstantValue.CONTACT_PHONE,phone);
            overridePendingTransition(R.anim.next_in_anim,R.anim.next_out_anim);
        }else {
            ToastUtil.show(this,"请输入电话号码！");
        }
    }

    @Override
    protected void showPrePage() {
        Intent intent = new Intent(getApplicationContext(), SetupTwoActivity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.pre_in_anim,R.anim.pre_out_anim);
    }

//    /**
//     * 1.“下一页”按钮的点击方法
//     * @param view
//     */
//    public void nextPage(View view){
//        // 点击按钮以后，需要获取输入框中的联系人，再做下一页操作
//        String phone = et_phone_number.getText().toString();
//        // sp中存储了相关联系人以后才可以跳转到下一个界面
//        // String contact_phone = SharedPreferencesUtil.getString(getApplicationContext(), ConstantValue.CONTACT_PHONE, "");
//        if (!TextUtils.isEmpty(phone)){
//            Intent intent = new Intent(getApplicationContext(), SetupFourActivity.class);
//            startActivity(intent);
//            finish();
//            // 如果是输入电话号码，同样需要保存
//            SharedPreferencesUtil.putString(getApplicationContext(), ConstantValue.CONTACT_PHONE,phone);
//            overridePendingTransition(R.anim.next_in_anim,R.anim.next_out_anim);
//        }else {
//            ToastUtil.show(this,"请输入电话号码！");
//        }
//    }

//    /**
//     * 2.“上一页”按钮的点击方法
//     * @param view
//     */
//    public void prePage(View view){
//        Intent intent = new Intent(getApplicationContext(), SetupTwoActivity.class);
//        startActivity(intent);
//        finish();
//        overridePendingTransition(R.anim.pre_in_anim,R.anim.pre_out_anim);
//    }

    /**
     * 3.初始化UI
     */
    private void initUI() {
        et_phone_number = findViewById(R.id.et_phone_number);
        // 回显过程
        String phone = SharedPreferencesUtil.getString(this, ConstantValue.CONTACT_PHONE, "");
        et_phone_number.setText(phone);
        btn_select_number = findViewById(R.id.btn_select_number);
        btn_select_number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), ContactListActivity.class);
                startActivityForResult(intent,0);
            }
        });
    }

    /**
     * 4.Activity回调
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (data != null){
            // 1.返回到当前界面后接收数据
            String phone = data.getStringExtra("phone");
            // 2.过滤特殊字符（中划线转换成空字符串）
            phone.replace("-","").replace(" ","").trim();
            et_phone_number.setText(phone);
            // 3.存储联系人
            SharedPreferencesUtil.putString(getApplicationContext(), ConstantValue.CONTACT_PHONE,phone);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
